{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：使用辅助列表，先将链表转换成列表，再构建二叉平衡搜索树,时间O(n),空间O(n)\n",
    "# Definition for singly-linked list.\n",
    "class ListNode:\n",
    "    def __init__(self, x):\n",
    "        self.val = x\n",
    "        self.next = None\n",
    "\n",
    "# Definition for a binary tree node.\n",
    "class TreeNode:\n",
    "    def __init__(self, x):\n",
    "        self.val = x\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "\n",
    "class Solution:\n",
    "    def sortedListToBST(self, head: ListNode) -> TreeNode:\n",
    "        nums = []\n",
    "        p = head\n",
    "        while(p):\n",
    "            nums.append(p.val)\n",
    "            p = p.next\n",
    "        def build(begin, end):\n",
    "            if(begin > end):\n",
    "                return None\n",
    "            mid = (begin+end+1)//2\n",
    "            root = TreeNode(nums[mid])\n",
    "            root.left = build(begin, mid-1)\n",
    "            root.right = build(mid+1, end)\n",
    "            return root\n",
    "        return build(0, len(nums)-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：链表逐级分裂,时间O(n),空间O(1)\n",
    "class Solution:\n",
    "    def sortedListToBST(self, head: ListNode) -> TreeNode:\n",
    "        if(head == None):\n",
    "            return None\n",
    "        if(head.next == None):\n",
    "            return TreeNode(head.val)\n",
    "        q = ListNode(0) # 虚拟头节点\n",
    "        q.next = head # q指向中间节点的前一节点\n",
    "        p = head\n",
    "        while(p and p.next):\n",
    "            p = p.next.next\n",
    "            q = q.next\n",
    "        cur = q.next\n",
    "        q.next = None\n",
    "        root = TreeNode(cur.val)\n",
    "        root.left = self.sortedListToBST(head)\n",
    "        root.right = self.sortedListToBST(cur.next)\n",
    "        return root"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
